package app.widget;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.longevityassistant.activity.R;

/**
 * 银行卡选择对话框
 * @author EX-XIAOFANQING001
 * @date 2012-1-6
 * @version [Android PABank C01, @2012-1-6]
 * @description
 */
public class MySpinnerDialog extends BaseDialog
{
    private static final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    
    private LinearLayout contentView;
    private ListView mListView;
    
    private OnMenuItemClickListener onMenuItemClickListener;
    
    private String[] mFrom;
    private int[] mTo;
    
    private int mLayoutResId;
    
    private List<Map<String, String>> mDates;
    
    public MySpinnerDialog(Context context, List<Map<String, String>> list, int layoutResId, String[] from, int [] to)
    {
        super(context); 
        
        mDates = list;
        mFrom = from;
        mTo = to;
        
        mLayoutResId = layoutResId;
        
        contentView =  new LinearLayout(context);
        contentView.setOrientation(LinearLayout.VERTICAL);
        
        mListView = new ListView(context);
        mListView.setCacheColorHint(0x00000000);
        mListView.setDivider(null);
        mListView.setFadingEdgeLength(0);
        lp.weight = 1;
        contentView.addView(mListView, lp);

        TextView tvLine = new TextView(context);
        tvLine.setBackgroundResource(R.drawable.app_widget_dialog_white_line);
        contentView.addView(tvLine, new LayoutParams(LayoutParams.FILL_PARENT, 1));
        
        MyAdapter adapter = new MyAdapter();
        mListView.setAdapter(adapter);
        
        mListView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                if (onMenuItemClickListener != null) {
                    onMenuItemClickListener.onMenuItemClick(position);
                }
                dismiss();
            }
        });
        
        setBtn1Visible(false);
        setBtn2Visible(false);
    
    }

    @Override
    public View createContentView()
    {
        return contentView;
    }
    
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount()
        {
            if (mDates != null) {
                return mDates.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position)
        {
            if (mDates != null) {
                return mDates.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view = LayoutInflater.from(getContext()).inflate(mLayoutResId, null);
            Map<String, String> data = mDates.get(position);
            TextView [] texts = new TextView[mTo.length];
            for (int i=0; i<texts.length; i++) {
                texts[i] = (TextView) view.findViewById(mTo[i]);
                texts[i].setText(data.get(mFrom[i]));
            }
            
            return view;
        }
    }
    
    public void setOnMenuItemClickListener(OnMenuItemClickListener onMenuItemClickListener)
    {
        this.onMenuItemClickListener = onMenuItemClickListener;
    }

    public interface OnMenuItemClickListener {
        public void onMenuItemClick(int position);
    }
}

